<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mmenus extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
   
    public function getMenusDB($number,$offset)
    {
        $limit = $offset ? intval($offset) : '0';
        $sql  = '';
        $sql = "SELECT rm.id, rm.`slide_image`, rm.`created`, rm.`active`, r.name, z.title FROM restaurant_menus as rm JOIN restaurant as r ON r.id = rm.restaurant_id JOIN zone as z ON z.id = rm.zone_id WHERE 1=1 ";
        $sql .= "ORDER BY r.orderby ASC, z.orderby ASC, rm.zone_id ASC";
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        return $xs;
        //foreach($xs AS $el=>$le)
       // {
         //   $xs[$el]->zone = $this->db->query('SELECT rz.`title`, rz.id FROM restaurant_address AS z INNER JOIN zone AS rz ON rz.id = z.id_zone WHERE z.id = '.$le->id)->result_object();
           // 
           // $xs[$el]->restaurant = $this->db->query('SELECT rck.`name`, rck.id  FROM restaurant_address AS z INNER JOIN restaurant AS rck ON rck.id = z.`restaurant_id` WHERE z.id = '.$le->id)->result_object();
       // }
        
        //$count = $this->db->query($sql)->num_rows();
        //return array('data' => $xs, 'count' => $count);   
    }
    
  
    
	public function optionZone($id)
    {
        $data = $this->db->query('SELECT id, title FROM zone ORDER BY title ASC')->result_object();
        $html = '<option value = "">Chọn khu vực</option>';
        foreach($data AS $el=>$le)
        {
            if($le->id == $id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$le->id.'" '.$selected.'>'.$le->title.'</option>';
        }
        return $html;
    }
	
    public function optionRestaurant($id)
    {
        $data = $this->db->query('SELECT id, name FROM restaurant ORDER BY id ASC')->result_object();
        $html = '<option value = "">Chọn nhà hàng</option>';
        foreach($data AS $el=>$le)
        {
            if($le->id == $id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$le->id.'" '.$selected.'>'.$le->name.'</option>';
        }
        return $html;
    }   
	
	public function addMenus($restaurant_id,$zone_id,$arr)
    {
		$db = $this->db->query('SELECT * FROM restaurant_menus WHERE restaurant_id = '.$restaurant_id.' AND zone_id = '.$zone_id.' LIMIT 1')->num_rows();
		if( $db != 0 ){
			return 201;
		}
		
        $data = array(
			'restaurant_id' => $restaurant_id,
			'zone_id' => $zone_id,
            'slide_image' => json_encode($arr),
			'created' => time(),
			'active' => 1
        );
        $this->db->insert('restaurant_menus', $data);
        return 200;
    }
	
	public function deleteMenusDb($id){
		$data_img = $this->db->query("SELECT * FROM restaurant_menus WHERE id = '".$id."'")->result_object();
		foreach($data_img AS $el=>$le){
            $idata = $le->slide_image;
			foreach ( json_decode($idata) AS $el1=>$le1 ){ 
				@unlink(PROJECT_PATH.'/data/'.$le1->image);
			}
		}
        $this->db->delete('restaurant_menus', array('id' => $id)); 
        return 200;
	}
	
	public function getMenusInfo($id){
		return $this->db->query("SELECT * FROM restaurant_menus WHERE id = '".$id."'")->row_object();
	}
	
	public function editMenus($id,$restaurant_id,$zone_id,$arr){
		 $data = array(
			'restaurant_id' => $restaurant_id,
			'zone_id' => $zone_id,
            'slide_image' => json_encode($arr)
        );
        $this->db->update('restaurant_menus', $data, "id = ".$id);
        return 200;
	}
	
	public function ajaxCheckAddmenus($zone_id,$res_id){
		$db = $this->db->query('SELECT * FROM restaurant_menus WHERE restaurant_id = '.$res_id.' AND zone_id = '.$zone_id.' LIMIT 1')->num_rows();
		if( $db == 0 ){
			return 201;
		}else{
			return 200;
		}
	}
}
 